
//
//  ScoreVC.m
//  One
//
//  Created by mac on 16/6/2.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ScoreVC.h"
#import "EFCircularSlider.h"
#import "ScoreObject.h"
#import "MovieDetailVC.h"



@interface ScoreVC ()
{
    NSMutableArray *ScoreObjArr;
    int current;
    NSString *str;
}
@end

@implementation ScoreVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
   
    [self getData];
    [self layoutLabel];
    ScoreObjArr = [[NSMutableArray alloc] init];
    CGRect sliderFrame = CGRectMake(WIDTH/2-120, HEIGHT/2-120, 240, 240);
    EFCircularSlider * circularSlider = [[EFCircularSlider alloc] initWithFrame:sliderFrame];
    circularSlider.handleType = bigCircle;
    circularSlider.filledColor = [UIColor greenColor];
    circularSlider.unfilledColor = [UIColor whiteColor];
    circularSlider.maximumValue = 100;
    circularSlider.minimumValue = 0;
    circularSlider.handleColor = [UIColor whiteColor];
    [self.view addSubview:circularSlider];
    [circularSlider addTarget:self action:@selector(ValueDidChange:) forControlEvents:UIControlEventValueChanged];
    
    UILabel *label = (UILabel *)[self.view viewWithTag:120];
    label.text = @"0";
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.navigationController.navigationBarHidden = YES;
}

- (void)layoutLabel
{
    for (int i = 0; i <2; i ++) {
        UILabel *label = [[UILabel alloc] init];
        label.center = CGPointMake(WIDTH/2 , HEIGHT/2-(i*160));
        label.bounds = CGRectMake(0, 0, 100, 40);
        label.font = Font(19);
        label.textColor = [UIColor whiteColor];
        
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = i + 120;
        [self.view addSubview:label];
    }
    
    UIButton *removeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [removeBtn setTitle:@"完成" forState:UIControlStateNormal];
    [removeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [removeBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:removeBtn];
    [removeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).with.offset(-20);
        make.left.equalTo(self.view).with.offset(60);
        make.size.mas_equalTo(CGSizeMake(100, 40));
    }];
}

- (void)btnClick
{
     str = [NSString stringWithFormat:@"%d",current];
    [self dismissViewControllerAnimated:NO completion:nil];
}


- (void)getData
{
    NSDictionary *para = @{URLKEY:@"movie/scoretext"};
    [HttpManager requestDataWithParameters:para requestMethod:@"GET" completionBlock:^(NSDictionary *responseDic) {
        NSArray *arr = responseDic[@"data"];
        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            ScoreObject *scoObj = [ScoreObject mj_objectWithKeyValues:obj];
            [ScoreObjArr addObject:scoObj];
        }];
    }];
}

- (void)ValueDidChange:(EFCircularSlider *)slieder
{
    NSLog(@"%f",slieder.currentValue);
    [ScoreObjArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        ScoreObject *scoObj = obj;
        
        NSArray *arr = [scoObj.range componentsSeparatedByString:@"-"];
        NSLog(@"%@",arr);
        current = slieder.currentValue;
        NSLog(@"%d",current);
        if (current >= [arr[0] intValue] &&  current <= [arr[1] intValue]) {
            UILabel *label1 = (UILabel *)[self.view viewWithTag:120];
            UILabel *label2 = (UILabel *)[self.view viewWithTag:121];
            label1.text = [NSString stringWithFormat:@"%d",current];
            label2.text = [NSString stringWithFormat:@"%@",scoObj.content];
        }
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)returnText:(returnTextBlock)textBlock
{
    textBlock(str);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
